package com.kkb.kkbadmin.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.kkbadmin.domain.Special;
import com.kkb.kkbadmin.service.SpecialService;
import com.kkb.kkbcommon.api.CommonPage;
import com.kkb.kkbcommon.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Administrator
 * @ClassName：SpecialController
 * @User：面向对象面向君，不负代码不负卿
 * @Data：2021/8/15
 * @Time：19:10
 */
@Api(tags = "首页特点类管理", value = "后台特点类模块")
@RestController
@RequestMapping("/home/special")
public class SpecialController {

    @Autowired
    private SpecialService specialService;

    /**
     * 分页查询
     *
     * @param special  条件
     * @param pageNum  页码
     * @param pageSize 每页显示行数
     * @return 分页数据
     */
    @ApiOperation(value = "特点类分页查询", notes = "特点类分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "special", value = "特点类条件", dataType = "Special"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数", defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", dataType = "Integer")
    })
    @GetMapping("/page")
    public CommonResult<CommonPage<Special>> listPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                                      Special special) {
        PageInfo<Special> page = specialService.findPage(special, pageNum, pageSize);
        CommonPage<Special> commonPage = CommonPage.restPage(page.getList());
        return CommonResult.success(commonPage);
    }

    @ApiOperation(value = "根据id查询", notes = "根据id查询特点类")
    @ApiImplicitParam(name = "specialId", value = "特点类id", required = true, dataType = "Long")
    @GetMapping("/{id}")
    public CommonResult<Special> getById(@PathVariable("id") Long specialId) {
        Special special = specialService.getBySpecialId(specialId);
        return CommonResult.success(special);
    }

    @ApiOperation(value = "添加特点类", notes = "添加一个特点类")
    @ApiImplicitParam(name = "special", value = "特点类对象", required = true, dataType = "Special")
    @PostMapping
    public CommonResult<Object> postSpecial(@Validated @RequestBody Special special) {
        specialService.addSpecial(special);
        return CommonResult.success(null, "添加成功");
    }

    @ApiOperation(value = "删除特点类", notes = "删除一个特点类")
    @ApiImplicitParam(name = "specialId", value = "特点类id", required = true, dataType = "Long")
    @DeleteMapping("/{id}")
    public CommonResult<Object> deleteSpecialById(@PathVariable("id") Long specialId) {
        specialService.deleteById(specialId);
        return CommonResult.success(null, "删除成功");
    }

    @ApiOperation(value = "修改特点类", notes = "根据特点类主键id修改特点类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "specialId", value = "特点类id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "special", value = "特点类对象", required = true, dataType = "Special")
    })
    @PutMapping("/{id}")
    public CommonResult<Object> putSpecialById(@PathVariable("id") Long specialId,
                                               @Validated @RequestBody Special special) {
        specialService.updateById(specialId, special);
        return CommonResult.success(null, "修改成功");
    }
}

